<?php
    #-------------------------------------------------------------#
	# This file is part of the Kumomado - Virtual Window Platform #
	# for PHP. (c) 2009 Tsai, Min Che <tirme0812@gmail.com>       #
	# Please visit http://www.kumomado.org/ for more information. #
	#                                                             #
	# This software is licensed under the GNU License v3. Please  #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#

	/**
	 * @package libraries/object/
	 * @author Tsai, Min Che
	 * @version 1.0
	 * @abstract
	 */
	abstract class KumomadoObject {
		/**
		 * @var array
		 */
		protected $vars;
		/**
		 * @var string
		 */
		protected $table;
		/**
		 * @var array
		 */
		protected $fields = null;
		/**
		 * @var string
		 */
		protected $primary_key = 'id';
		/**
		 *
		 * @param string $var_name
		 * @param boolean $is_primary_key
		 */
		protected function initVar($var_name, $is_primary_key = false) {
			$this->vars[$var_name] = array(
				'value' => null
			);
			if ($is_primary_key) {
				$this->setPrimaryKey($var_name);
			}
		}
		public function addVar($var_name) {
			if (!in_array($var_name, $this->getFields())) {
				$this->vars[$var_name] = array(
					'value' => null,
					'add' => true
				);
			}
		}
		/**
		 *
		 * @param string $var_name
		 * @param mixed $value
		 */
		public function setVar($var_name, $value) {
			if (isset($this->vars[$var_name])) {
				$this->vars[$var_name]['value'] = $value;
			}
		}
		/**
		 *
		 * @param array $var_array
		 */
		public function setVars($var_array) {
			if (is_array($var_array)) {
				foreach ($var_array as $var_name => $value) {
					$this->setVar($var_name, $value);
				}
			}
		}
		/**
		 *
		 * @param string $var_name
		 * @return mixed
		 */
		public function getVar($var_name) {
			return isset($this->vars[$var_name]) ? $this->vars[$var_name]['value'] : null;
		}
		/**
		 *
		 * @return array
		 */
		public function getVars() {
			$values = array();
			foreach ($this->vars as $var_name => $var) {
				$values[$var_name] = $var['value'];
			}
			return $values;
		}
		/**
		 * Clear variables value
		 */
		public function clear() {
			foreach ($this->vars as $var_name => $var) {
				$this->setVar($var_name, null);
			}
		}
		/**
		 *
		 * @param string $table
		 */
		protected function setTable($table) {
			$this->table = $table;
		}
		/**
		 *
		 * @return string
		 */
		public function getTable() {
			return $this->table;
		}
		/**
		 * Get table's fields from variables key
		 * @return array
		 */
		public function getFields() {
			if ($this->fields == null) {
				$this->fields = array_keys($this->vars);
			}
			return $this->fields;
		}
		/**
		 *
		 * @param string $primary_key
		 */
		protected function setPrimaryKey($primary_key) {
			if (in_array($primary_key, $this->getFields())) {
				$this->primary_key = $primary_key;
			} else {
				throw new Exception('pk is not exists');
			}
		}
		/**
		 *
		 * @return string
		 */
		public function getPrimaryKey() {
			return $this->primary_key;
		}
		/**
		 *
		 * @staticvar object $handler @link KumomadoObjectHandler
		 * @return object
		 */
		public function handler() {
			static $handler = null;
			if ($handler == null) {
				$handler = new KumomadoObjectHandler($this);
			}
			return $handler;
		}
		/**
		 *
		 * @param string $var_name
		 * @param mixed $value
		 */
		public function __set($var_name, $value) {
			$this->setVar($var_name, $value);
		}
		/**
		 *
		 * @param string $var_name
		 * @return mixed
		 */
		public function __get($var_name) {
			return $this->getVar($var_name);
		}
		/**
		 *
		 * @param string $var_name
		 * @return boolean
		 */
		public function __isset($var_name) {
			return isset($this->vars[$var_name]);
		}
		/**
		 *
		 * @param string $var_name
		 */
		public function __unset($var_name) {
			$this->setVar($var_name, null);
		}
		/**
		 *
		 * @param string $method
		 * @param array $args
		 * @return mixed
		 */
		public function __call($method, $args) {
			return call_user_func_array(array($this->handler(), $method), $args);
		}
	}
?>